import JsonSchemaEditorFunc from "@/components/JsonSchemaForm/JsonSchemaFormFunc.vue";
import { SchemaNodeKey } from "@/utils/constants";
import mittBus from "@/utils/mittBus";
import { parseProfileFormFunc, parseSchemaFromFunc } from "@/utils/schema";
import { showMsg, showTips } from "@/utils/util";
import { getCurrentInstance, defineComponent, ComponentInternalInstance } from "vue";
import { useRoute } from "vue-router";
import { SchemaInfo } from "@/components/JsonSchemaForm/interface";
import { GatewayType } from "@/api/interface/iot";
import { funcMod } from "@/api/modules/iot/variable";
import { sendFunc } from "@/api/modules/iot/device";

/**
 * 设备参数信息,scheme中的config下面子设备参数信息（排除sensor节点）
 * 相关参数：设备接入端口、地址、采集周期、上报周期
 */
export default defineComponent({
	props: {
		type: {
			type: String,
			default() {
				return GatewayType.Device;
			}
		}
	},
	emits: [],
	setup() {
		const { proxy } = getCurrentInstance() as ComponentInternalInstance;
		const route = useRoute();
		const refreshDeviceDetail = () => {
			if (route.query.id) {
				mittBus.emit("refreshProductDetail");
			} else if (route.query.type) {
				mittBus.emit("refreshGatewayDetail");
			} else {
				mittBus.emit("refreshDeviceDetail");
			}
		};
		const jsonRoot = reactive({
			root: {
				type: "object",
				title: "",
				description: "",
				properties: {},
				required: []
			}
		});
		const saveSchema = data => {
			if (typeof data.data == "object") {
				data.data = null;
			}
			funcMod({ entity: route.query.sn || route.query.id, ...data }).then(res => {
				let ok = showTips(res.code, proxy?.$t("funclist.tips.device"), res.message);
				if (ok) {
					// refreshDeviceDetail();
				}
			});
		};

		//设备参数配置模型
		const schema = ref(inject<any>(SchemaNodeKey.schema));
		const profile = ref<any>(inject<any>(SchemaNodeKey.profile));
		//缓存sensor数据
		let profileFunc = ref({});
		let schemaFunc = ref({});
		onBeforeMount(() => {
			refreshDeviceDetail();
			initSensor();
			mittBus.on("addJsonValue", data => {
				saveSchema(data);
			});
			mittBus.on("clickValueIcon", (data: any) => {
				sendFunc({
					idSn: route.query.sn || route.query.id,
					funcName: data.dataPath,
					data: data.dataValue
				}).then((res: any) => {
					showMsg(res.message, res.code == 200);
				});
			});
		});
		onBeforeUnmount(() => {
			mittBus.off("addJsonValue");
			mittBus.off("clickValueIcon");
		});
		watch(
			() => schema.value,
			() => {
				initSensor();
			}
		);
		const initSensor = () => {
			profileFunc.value = parseProfileFormFunc(profile.value);
			schemaFunc.value = parseSchemaFromFunc(schema.value);
			let funcRoot = schemaFunc.value as SchemaInfo;
			jsonRoot.root.title = funcRoot.title;
			jsonRoot.root.description = funcRoot.description;
			jsonRoot.root.properties = funcRoot.properties || {};
		};

		// const dialogVisible = ref<boolean>(false);
		return () => {
			return (
				<div class="block-basic p-8">
					{/* <DialogBox
						title="查看JSON"
						class="dialog-dense"
						v-model:visible={dialogVisible.value}
						footerLabel={["关闭"]}
						width="550px"
					>
						<JsonView profile={profileFunc.value} schema={jsonRoot.root} />
					</DialogBox> */}

					<JsonSchemaEditorFunc class="w-full mb-4" value={jsonRoot} lang="zh_CN" custom jsonValue={profileFunc.value} />
				</div>
			);
		};
	}
});
